#!/bin/bash
#功能函数
funcs=`which functions-svn.sh`
if [ "$funcs" != "" ] ;then
    . $funcs
else
    funcs=`dirname $0`/functions-svn.sh
    echo "$PWD"
    echo "funcs=$funcs"
    if [ "$funcs" != "" ] ;then
	. $funcs
    else
	exit
    fi
fi

###########################################
CP="cp -rvf"
PARSEINI=`which parseini.shc`
projects_ini=/tmp/projects.ini
#result=`get-config.shc http://\`sh_get_svn_hostip\`/repositories/DocsAndTools/scripts/configs/public/projects.ini $projects_ini`
result=`get-config.shc http://smartphone/repositories/DocsAndTools/scripts/configs/public/projects.ini $projects_ini`
###########################################

doit()
{
    oldname=`ls | grep "$1$"`
    for name in $oldname
    do
       if [ -d "$name" ]
       then :
       else
            prename=${name%.*}
            echo -e "$PWD/$name\t\t$newname"
            mv $name $newname
            count=`expr ${count} + 1`
       fi
    done

    return 0
}


do_recursive()
{
    doit $1 $2
    for filename in `ls`
    do
         if [ -d "$filename" ]
         then
             cd $filename
             do_recursive $1 $2
             cd ..
         fi
    done
    return 0
}

modify()
{
    PARAMS=2
    if [ $# -ne $PARAMS ]
    then
        echo "usage: mv_to .suf1 .suf2"
        return 1
    fi
    count=0
    do_recursive $1 $2
    echo "complete! $count files have been modified."

    return 0
}

do_recursive2()
{
    local ac_pwd=`pwd`
    if test -f $ac_pwd/release.xml; then
      echo "$ac_pwd/release.xml"
    fi
    for filename in `svn.shc list    $app_url`
    do
         if [ -d "$filename" ]
         then
             cd $filename
             do_recursive2
             cd ..
         fi
    done
    return 0
}

do_recursive3()
{
    local app_url=$1
    result=`svn.shc list $app_url  |grep $2`
    if [ -n "$result" ]; then
      echo "$app_url/$result">&2
      echo "$app_url/$result"
      return
    fi
    for ac_dir in `svn.shc list $app_url |grep /`
    do
      app_url=$app_url/$ac_dir
     # echo "aaa=$app_url"
      do_recursive3 $app_url $2
      app_url=`dirname $app_url`
    done
    return 0
}


function sh_get_project_list()
{
  local projects_name=
  local projects_tags=
  local project_config_fs=
  projects_tags=`cat $projects_ini |grep "\[*\]" |tr -d '[|]'|tr '\n' ' '`
  projects_tags=`echo $projects_tags | sed 's/global//g'`
  for f in $projects_tags
  do
  ac_name="`$PARSEINI "$projects_ini" get "$f" "project_name"`"
  ac_des="`$PARSEINI "$projects_ini" get "$f" "project_des"`"
  if [ -z "$ac_des" ]; then
    ac_tags="$ac_tags  ${f}_${ac_name}"
  else
    ac_tags="$ac_tags  ${f}_${ac_name}_${ac_des}"
  fi
  projects_name="$projects_name $ac_name"
  done
  echo $projects_name
}

############全局变量
project_list="`sh_get_project_list`"
############




function reinit_project_info()
{
  local ac_dir=`mktemp -d`/packages/
  local apps_url=$1
  local project_name=$2


  echoTextBlue "apps_url=$apps_url"
  echoTextBlue "project_name=$project_name"
  echoTextBlue "从服务器获取 release.xml 信息......"
  release_xml_file_list=`do_recursive3  $apps_url  release.xml`

  release_all=$expansion_options/release_all.txt
  release_tmp=/tmp/release_tmp.txt
  rm -rf $release_all $release_tmp

  for xml in $release_xml_file_list
  do
    xml1=`echo $xml|sed "s#$apps_url/##g"`
    xml2=`dirname $xml1`
    echo $xml2>>$release_tmp
    mkdir -p $ac_dir/$xml2
    svn.shc cat $xml  > $ac_dir/$xml2/release.xml
  done
  cat  $release_tmp | sort > $release_all

  release_default=/tmp/release_default.txt
  release_project=/tmp/release_${project_name}.txt
  release_tmp=/tmp/release_tmp.txt 
  rm -rf $release_default $release_project $release_tmp
  echoTextBlue "生成应用配置信息......"
  release_xml_file_list=`findleaves.py $ac_dir release.xml`
  for xml in $release_xml_file_list
  do
    xml_dir=`dirname $xml`
    pkg_dir_name=`basename $xml_dir`

    #echo "$xml|sed \"s#$ac_dir##g\""
    xml1=`echo $xml|sed "s#$ac_dir##g"`
    xml1=`dirname $xml1`

    echo "svn-update-get-pkgs.py $xml default  package_dir"
    result=`svn-update-get-pkgs.py $xml default  package_dir`
    if [ -n "$result" ]; then
      for f in $result
      do
        echo "$xml1/$f">> $release_default
      done
    fi
    #echo "svn-update-get-pkgs.py $xml $project_name  package_dir"
    result=`svn-update-get-pkgs.py $xml $project_name  package_dir`
    if [ -n "$result" ]; then
      for f in $result
      do
        echo "$xml1/$f>> $release_project"
        echo "$xml1/$f">> $release_project
      done
    fi
  done
  if test -f $release_default; then
    cat $release_default > $release_tmp ; cat $release_tmp | sort > $release_default
  fi
  if test -f $release_project ; then  
    cat $release_project > $release_tmp ; cat $release_tmp | sort > $release_project
  fi
  rm $ac_dir -rf
}


special_package_list="
Camera
"

function svn_update2()
{
  local url=$1
  local work_dir=$2
  local version=$3
  local is_special_pkg=false
  local url1=`dirname $url`
  local url2=`basename $url`
  if [ -z "$work_dir" ]; then
    work_dir=`pwd`
  fi
  rm -rf $work_dir
  mkdir -pv $work_dir
  local file_url_warning=$work_dir/.file_url_warning.txt
  local file_url_not_exist=$work_dir/.file_url_not_exist.txt
  local file_pkg_checkout=$work_dir/.file_pkg_check_out.txt
  local file_pkg_update=$work_dir/.file_pkg_update.txt
  local file_pkg_should_rm=$work_dir/.file_pkg_should_rm.txt


  rm $file_url_warning -rf; touch $file_url_warning
  rm $file_url_not_exist -rf; touch $file_url_not_exist
  rm $file_pkg_checkout -rf; touch $file_pkg_checkout
  rm $file_pkg_update -rf; touch $file_pkg_update
  rm $file_pkg_should_rm -rf; touch $file_pkg_should_rm

  local release_xml_tmp=$work_dir/temp_release.xml
  url=$url1/$url2
 
  reinit_project_info  $url $project_name


  echo "calling svn_update ....."
  package_list=`cat /tmp/release_${project_name}.txt`
  echo "`sh_get_svn_committed_no_from_url $url`"
  if [ -n "$VERSION" ]; then
     echo $VERSION >$work_dir/.version
     echo "sed -i "/current_app_version/s/.*/current_app_version:${VERSION}/g" $work_dir/../svn_version"
     sed -i "/current_app_version/s/.*/current_app_version:${VERSION}/g" $work_dir/../svn_version
  else
    echo "`sh_get_svn_committed_no_from_url $url`">$work_dir/.version
    new_app_version=`cat $work_dir/.version`  
    echo "sed -i "/current_app_version/s/.*/current_app_version:${new_app_version}/g" $work_dir/../svn_version"
     sed -i "/current_app_version/s/.*/current_app_version:${new_app_version}/g" $work_dir/../svn_version
  fi


  for pkg in $package_list
  do
    ac_pkg=`dirname $pkg`
    special_pkg_name=`basename $ac_pkg`
    ac_pkg=`dirname $ac_pkg`
    ac_pkg_name=`basename $pkg`
    for special_pkg in $special_package_list
    do
      if [ "$special_pkg" = "$special_pkg_name" ]; then
        is_special_pkg=true
        break;
      else
        is_special_pkg=false
      fi
    done
    if [ "$is_special_pkg" = "true" ]; then
      ac_pkg=$ac_pkg/$special_pkg_name
    else
      ac_pkg=$ac_pkg/$ac_pkg_name
    fi

    pkg_release_xml_url=`dirname $url/$pkg`/release.xml
    #echo "pkg_release_xml_url=$pkg_release_xml_url"
    pkg_release_xml=`svn.shc list  $pkg_release_xml_url $ac_VERSION 2>/tmp/error.txt` 
    #echo "pkg_release_xml=$pkg_release_xml"
    if [ "$pkg_release_xml" = "release.xml" ]; then
      svn.shc cat  $pkg_release_xml_url $ac_VERSION>$release_xml_tmp
      #package_dir=`svn.shc_update_apps.py  $release_xml_tmp  $project_name  package_dir`
      local package_ver=
      local ac_version=
      echo "svn-update-get-pkg-version.py  $release_xml_tmp  $project_name  version $ac_pkg_name"
      package_ver=`svn-update-get-pkg-version.py  $release_xml_tmp  $project_name  version $ac_pkg_name`
      if [ -n "$package_ver" ]; then
         ac_version="-r $package_ver"
      else
        if [ -n "$ac_VERSION" ]; then
          ac_version="$ac_VERSION"
        else
          ac_version="-r head"
        fi
      fi   
      #echo "pkg=$pkg ac_pkg=$ac_pkg"
      pkg_release=`svn.shc list  $url/$pkg  2>/tmp/error.txt`
      if [ -n "$pkg_release" ]; then
        if test -d $work_dir/$ac_pkg; then
          if test -d $work_dir/$ac_pkg/.svn; then
            ac_url=`sh_get_svn_url $work_dir/$ac_pkg`
            if [ "$url/$pkg" = "$ac_url" ]; then
              echo "updating $work_dir/$ac_pkg from $ac_url"
	      svn.shc up $work_dir/$ac_pkg  $ac_version
              version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
              echo $work_dir/$ac_pkg $version_committed >> $file_pkg_update

         ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $ac_url`
   	      echo "$ac_url $local_ver" >> ./packageCheckout.sh 
	 ############## end added by wuquan ####
            else
              echoTextRed "warnning $work_dir/$ac_pkg"
              result=`question "$url/$pkg != $ac_url 是否中止？" 1 yes`
              if [  "$result" = "yes" ] ; then
                return
              fi
              echo $work_dir/$ac_pkg>>$file_url_warning
            fi         
          else          
            echo "checking $work_dir/$ac_pkg from $url/$pkg"
            svn.shc co $url/$pkg $work_dir/$ac_pkg  $ac_version
            version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
            echo $work_dir/$ac_pkg  $version_committed >> $file_pkg_checkout
	     ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $url/$pkg`
   	      echo "$url/$pkg $local_ver" >> ./packageCheckout.sh 
	    ############## end added by wuquan ####
          fi
        else
          echo "checking $work_dir/$ac_pkg from $url/$pkg"
          svn.shc co $url/$pkg $work_dir/$ac_pkg  $ac_version
          version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
          echo $work_dir/$ac_pkg $version_committed>> $file_pkg_checkout
	     ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $url/$pkg`
   	      echo "$url/$pkg $local_ver" >> ./packageCheckout.sh 
	    ############## end added by wuquan ####
        fi
      else
        echo $url/$pkg>>$file_url_not_exist
      fi
    fi
  done

  package_list2=`findleaves.py packages/ Android.mk |sed 's#/Android.mk##g'`
  for pkg in $ $package_list2
  do
    ac_pkg=`basename $pkg`
    result=`echo $package_list |grep $ac_pkg`
    if [ -z "$result" ]; then
      echo $pkg>>$file_pkg_should_rm
    fi
  done

  if test -f $file_url_not_exist; then
    echoTextRed "-------以下包不存在------"
    cat $file_url_not_exist
    echoTextRed "------------------"
  fi

  if test -f $file_pkg_checkout; then
    echoTextRed "-------checkout 的包------"
    cat $file_pkg_checkout
    echoTextRed "------------------"
  fi
  if test -f $file_pkg_update; then
    echoTextRed "-------更新的包-----"
    cat $file_pkg_update
    echoTextRed "------------------"
  fi
  if test -f $file_url_warning; then
    echoTextRed "-------警告包------"
    cat $file_url_warning
    echoTextRed "------------------"
  fi
  if test -f $file_pkg_should_rm; then
    echoTextRed "-------需要删除的包------"
    result=`cat $file_pkg_should_rm`
    if [ -n "$result" ]; then
      echoTextGreen "包：$result 将被删除!!"
      rm -rf $result
    else
      echoTextGreen "没有包需要删除"
    fi
    echoTextRed "------------------"
  fi
}


function svn_update_common()
{
  local url=$1
  local work_dir=$2
  local version=$3
  local url1=`dirname $url`
  local url2=`basename $url`
  if [ -z "$work_dir" ]; then
    work_dir=`pwd`
  fi
  mkdir -pv $work_dir
  local file_url_warning=$work_dir/.file_url_warning.txt
  local file_url_not_exist=$work_dir/.file_url_not_exist.txt
  local file_pkg_checkout=$work_dir/.file_pkg_check_out.txt
  local file_pkg_update=$work_dir/.file_pkg_update.txt
  local file_pkg_should_rm=$work_dir/.file_pkg_should_rm.txt


  rm $file_url_warning -rf; touch $file_url_warning
  rm $file_url_not_exist -rf; touch $file_url_not_exist
  rm $file_pkg_checkout -rf; touch $file_pkg_checkout
  rm $file_pkg_update -rf; touch $file_pkg_update
  rm $file_pkg_should_rm -rf; touch $file_pkg_should_rm

  local release_xml_tmp=$work_dir/temp_release.xml
  url=$url1/$url2
 
  reinit_project_info  $url $project_name


  echo "calling svn_update ....."
  package_list=`cat /tmp/release_${project_name}.txt`
  echo "`sh_get_svn_committed_no_from_url $url`"
  echo "`sh_get_svn_committed_no_from_url $url`">$work_dir/.version


  for pkg in $package_list
  do
    ac_pkg=`dirname $pkg`
    ac_pkg=`dirname $ac_pkg`
    ac_pkg_name=`basename $pkg`
    ac_pkg=$ac_pkg/$ac_pkg_name
    pkg_release_xml_url=`dirname $url/$pkg`/release.xml
    #echo "pkg_release_xml_url=$pkg_release_xml_url"
    pkg_release_xml=`svn.shc list  $pkg_release_xml_url $ac_VERSION 2>/tmp/error.txt` 
    #echo "pkg_release_xml=$pkg_release_xml"
    if [ "$pkg_release_xml" = "release.xml" ]; then
      svn.shc cat  $pkg_release_xml_url $ac_VERSION>$release_xml_tmp
      #package_dir=`svn.shc_update_apps.py  $release_xml_tmp  $project_name  package_dir`
      local package_ver=
      local ac_version=
      echo "svn-update-get-pkg-version.py  $release_xml_tmp  $project_name  version $ac_pkg_name"
      package_ver=`svn-update-get-pkg-version.py  $release_xml_tmp  $project_name  version $ac_pkg_name`
      if [ -n "$package_ver" ]; then
         ac_version="-r $package_ver"
      else
        if [ -n "$ac_VERSION" ]; then
          ac_version="$ac_VERSION"
        else
          ac_version="-r head"
        fi
      fi   
      #echo "pkg=$pkg ac_pkg=$ac_pkg"
      pkg_release=`svn.shc list  $url/$pkg  2>/tmp/error.txt`
      if [ -n "$pkg_release" ]; then
        if test -d $work_dir/$ac_pkg; then
          if test -d $work_dir/$ac_pkg/.svn; then
            ac_url=`sh_get_svn_url $work_dir/$ac_pkg`
            if [ "$url/$pkg" = "$ac_url" ]; then
              echo "updating $work_dir/$ac_pkg from $ac_url"
	      svn.shc up $work_dir/$ac_pkg  $ac_version
              version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
              echo $work_dir/$ac_pkg $version_committed >> $file_pkg_update

         ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $ac_url`
   	      echo "$ac_url $local_ver" >> ./packageCheckout.sh
	 ############## end added by wuquan ####
            else
              echoTextRed "warnning $work_dir/$ac_pkg"
              result=`question "$url/$pkg != $ac_url 是否中止？" 1 yes`
              if [  "$result" = "yes" ] ; then
                return
              fi
              echo $work_dir/$ac_pkg>>$file_url_warning
            fi         
          else          
            echo "checking $work_dir/$ac_pkg from $url/$pkg"
            svn.shc co $url/$pkg $work_dir/$ac_pkg  $ac_version
            version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
            echo $work_dir/$ac_pkg  $version_committed >> $file_pkg_checkout
	     ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $url/$pkg`
   	      echo "$url/$pkg $local_ver" >> ./packageCheckout.sh 
	    ############## end added by wuquan ####
          fi
        else
          echo "checking $work_dir/$ac_pkg from $url/$pkg"
          svn.shc co $url/$pkg $work_dir/$ac_pkg  $ac_version
          version_committed=`sh_get_svn_committed_no_from_copy $work_dir/$ac_pkg`
          echo $work_dir/$ac_pkg $version_committed>> $file_pkg_checkout
	     ############ added by wuquan ####
	      local_ver=`sh_get_svn_committed_no_from_url $url/$pkg`
   	      echo "$url/$pkg $local_ver" >> ./packageCheckout.sh
	    ############## end added by wuquan ####
        fi
      else
        echo $url/$pkg>>$file_url_not_exist
      fi
    fi
  done

  package_list2=`findleaves.py packages/ Android.mk |sed 's#/Android.mk##g'`
  for pkg in $ $package_list2
  do
    ac_pkg=`basename $pkg`
    result=`echo $package_list |grep $ac_pkg`
    if [ -z "$result" ]; then
      echo $pkg>>$file_pkg_should_rm
    fi
  done

  if test -f $file_url_not_exist; then
    echoTextRed "-------以下包不存在------"
    cat $file_url_not_exist
    echoTextRed "------------------"
  fi

  if test -f $file_pkg_checkout; then
    echoTextRed "-------checkout 的包------"
    cat $file_pkg_checkout
    echoTextRed "------------------"
  fi
  if test -f $file_pkg_update; then
    echoTextRed "-------更新的包-----"
    cat $file_pkg_update
    echoTextRed "------------------"
  fi
  if test -f $file_url_warning; then
    echoTextRed "-------警告包------"
    cat $file_url_warning
    echoTextRed "------------------"
  fi
  if test -f $file_pkg_should_rm; then
    echoTextRed "-------需要删除的包------"
    result=`cat $file_pkg_should_rm`
    if [ -n "$result" ]; then
      echoTextGreen "包：$result 将被删除!!"
      rm -rf $result
    else
      echoTextGreen "没有包需要删除"
    fi
    echoTextRed "------------------"
  fi
}

function svn_checkout()
{
  echo "check out ......"
}

function usege()
{
echoTextGreen "
$0 --url http://smartphone/repositories/AppsOfMtk/trunk/packages-w1140/  packages/ -p PD1110
$0 --url http://svn/repositories/AppsOfMtk/trunk/packages   -p PD1110 packages/
"
}


##########
while test $# != 0
do
    case $1 in
	--username)
	    shift
	    username=$1
	    ;; 
	--password)
	    shift
	    password=$1
	    ;; 
	--work_copy)
	    shift
	    work_copy=$1
	    ;;
	-m)
	    shift
	    mode=$1
	    ;; 
        --url)
	    shift
	    url=$1
	    ;;  
        -url)
	    shift
	    url=$1
	    ;; 
	-p)
	    shift
	    project_name=$1
	    ;;
	-t)
	    shift
	    TARGET_PRODUCT=$1
	    ;;
        -R)
	    shift
	    VERSION=$1
	    ;;

        -r)
	    shift
	    version=$1
	    ;;
   	-h)
	    shift
	    hardware=$1
	    ;;
        *)
	    expansion_options=$1
	   ;;
       esac
       shift
done
if [ "$mode" = "help" ] || [ "$mode" = "h" ]; then
  usege $0
  exit
fi

if [ -n "$expansion_options" ]; then
	work_dir=$expansion_options
else
	work_dir=`pwd`
	expansion_options="`pwd`"
fi

  ###
if [ "$mode" = "modem" ]; then
	modem_list=`ls $work_copy/mediatek/custom/common/modem/`
	if [ "$hardware" != "" ]; then
    		modem_name=`cat $work_copy/mediatek/config/$TARGET_PRODUCT/ProjectConfig_${hardware}* | grep "^CUSTOM_MODEM =" | awk '{print $3}'`
	else
    		modem_name=`sh_menu_select "$modem_list"`
	fi
	if [ "$modem_name" != "" ];then
        	svn_update_modem $work_copy/mediatek/custom/common/modem/$modem_name $version
		echo "$modem_name" > .modem_name
  	else
    		echo "无法获取modem文件夹名，退出！！！"
  	fi
  	exit
fi

if [ "$mode" = "apk" ]; then
	svn_update_ThirdParty $work_copy
	exit
fi

  ###


if [ -n "$VERSION" ]; then
    ac_VERSION="-r $VERSION"
fi
if [ -n "$project_name" ]; then
    project_name=`sh_menu_select "$project_list" "" $project_name`
else
    project_name=`sh_menu_select "$project_list"`
fi

  ##########
  if [ -n "$url" ]; then
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        url1=`dirname $url`
        url2=`basename $url`
        url=$url1/$url2
        if [ "$url" = "`sh_get_svn_url $work_dir`" ]; then
    echo "svn.shc up $url $work_dir"
    svn_update2 $url  $work_dir $version
        else
    echoTextRed "$work_dir 不是指定url: $url 的工作拷贝!!"
    echoTextGreen "$work_dir url: `sh_get_svn_url $work_dir`"
    exit
        fi
      else
        svn_update2 $url  $work_dir $version
      fi
    else
      echo "svn.shc co $url $work_dir"
      svn_update2 $url  $work_dir $version
    fi
  else
    if test -d $work_dir; then
      if test -d $work_dir/.svn; then
        url=`sh_get_svn_url $work_dir`
        echo "svn.shc up $url $work_dir"
        svn_update2 $url  $work_dir $version
      fi
    fi
  fi

exit

编译java资源
javac ./com/android/internal/Manifest.java ./com/android/internal/R.java ./android/Manifest.java -d  bin/
生成jar包
jar cvf framework-res.jar -C bin/ .

